/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package au.edu.swin.unilearn.students;

import au.edu.swin.unilearn.submission.Submission;
import au.edu.swin.unilearn.submission.SubmissionEntity;
import au.edu.swin.unilearn.tutorialGroup.TutorialGroup;
import au.edu.swin.unilearn.tutorialGroup.TutorialGroupEntity;
import au.edu.swin.unilearn.tutorialGroup.TutorialGroupPK;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author DidierLeung
 */
@Stateless
public class StudentSessionBean implements StudentSessionRemote
{
    @PersistenceContext
    private EntityManager em;

    public void addStudent(Student s)
    {
        StudentEntity aStudentEntity = new StudentEntity();
        aStudentEntity.setStudentEntityData(s);
        em.persist(aStudentEntity);
    }

    public List<Student> getAllStudents()
    {
        List<Student> results = new ArrayList<Student>();
        Query allStudentsQuery = em.createNamedQuery("findAllStudents");
        List dbResults = allStudentsQuery.getResultList();
        for (int i = 0; i < dbResults.size(); i++)
        {
            StudentEntity aStudentEntity = (StudentEntity) dbResults.get(i);
            results.add(aStudentEntity.getStudentObject());
        }
        return results;
    }

    public Student getStudent(String id)
    {
        StudentEntity aStudentEntity = em.find(StudentEntity.class, id);
        return aStudentEntity.getStudentObject();
    }

    public Boolean checkPassword(String id, String password)
    {
        Student aStudent = this.getStudent(id);
        return password.equals(aStudent.getPassword());
    }

    public List<Submission> getSubmissions(String id)
    {
        List<Submission> submissionCollection = new ArrayList();
        StudentEntity se = em.find(StudentEntity.class, id);
        List<SubmissionEntity> submissionEntityCollection = se.getSubmissionEntityCollection();
        for (int i = 0; i < submissionEntityCollection.size(); i++)
        {
            SubmissionEntity submissionEntity = submissionEntityCollection.get(i);
            submissionCollection.add(submissionEntity.getSubmissionObject());
        }
        return submissionCollection;
    }

    public void enrolStudent(String id, String subjectCode, Integer activityNo)
    {
        StudentEntity studentEntity = em.find(StudentEntity.class, id);
        TutorialGroupPK pk = new TutorialGroupPK(activityNo, subjectCode);
        TutorialGroupEntity tge = em.find(TutorialGroupEntity.class, pk);
        studentEntity.enrolStudent(tge);
    }

    public List<TutorialGroup> getEnrolledTutorialGroups(String studentID)
    {
//        System.out.println("Call to getEnrolledTutorialGroups Method");
        StudentEntity studentEntity = em.find(StudentEntity.class, studentID);
        List<TutorialGroupEntity> tgeCollection = studentEntity.getTutorialGroupEntityCollection();
        List<TutorialGroup> tutorialGroupCollection = new ArrayList();
        for(int i=0; i<tgeCollection.size(); i++)
        {
            TutorialGroupEntity tge = tgeCollection.get(i);
            tutorialGroupCollection.add(tge.getTutorialGroupObject());
//            System.out.println("Tutorial Group No: " + tge.getActivity_no() + 
//                                " Subject Code: " + (tge.getSubjectEntity()).getSubject_code());
        }
//        System.out.println("TutorialGroupCollection Size: " + tutorialGroupCollection.size());
        return tutorialGroupCollection;
    }
    
    public Boolean isInTutorialClass(String studentId, Integer activityNo, String subjectCode)
    {
        StudentEntity se = em.find(StudentEntity.class, studentId);
        List<TutorialGroupEntity> tutorialClass = se.getTutorialGroupEntityCollection();
        for(TutorialGroupEntity tge : tutorialClass)
        {
            if(activityNo.equals(tge.getActivity_no()) && subjectCode.equals(tge.getSubject_code()))
            {
                return true;
            }
        }
        return false;
    }
}
